//
//  HJCityController.m
//  KaiChat
//
//  Created by lx on 2023/12/20.
//

#import "HJCityController.h"
#import "HJSelectCityCell.h"
#import "HJSectionHeaderView.h"
#import "QMUIButton.h"
#import "HJCityController.h"
#import "HJMinePresent.h"
#import "HJUserModel.h"

@interface HJCityController ()

@property (nonatomic, strong) NSArray *datas;
@property (nonatomic, strong) HJCellModel *selectModel;

@end

@implementation HJCityController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationBar.title = @"Region";
    
    HJSectionHeaderView *regionsSectionView = [[HJSectionHeaderView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 20)];
    regionsSectionView.titleLabel.text = @"All regions";
    regionsSectionView.contentView.backgroundColor = UIColor.tj_F6F6F6Color;
    regionsSectionView.backgroundColor = UIColor.tj_F6F6F6Color;
 
    self.tableView.tableHeaderView = regionsSectionView;
    [self.tableView registerClass:HJSelectCityCell.class forCellReuseIdentifier:@"HJSelectCityCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight = 60;
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, TabbarSafeBottomMargin, 0);
    self.tableView.backgroundColor = UIColor.whiteColor;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.offset(KNavBarHight);
        make.bottom.offset(0);
    }];
    
    [self loadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJSelectCityCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HJSelectCityCell" forIndexPath:indexPath];
    cell.model = self.datas[indexPath.row];
    if ([cell.model.detailText isEqualToString:@"Selected"]) {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    } else {
        cell.selectionStyle = UITableViewCellSelectionStyleDefault;
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    HJCellModel *model = self.datas[indexPath.row];
    if (model.isSelect) return;
    
    [self.datas enumerateObjectsUsingBlock:^(HJCellModel *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.isSelect = NO;
    }];
    
    model.isSelect = YES;
    self.selectModel = model;
    [self.tableView reloadData];

    [HJHUD showLoadingInView:self.view];
    NSString *value = [NSString stringWithFormat:@"%@, %@", model.text, self.selectRegion];
    [HJMinePresent modifyRegion:value complete:^(NSError * _Nullable error) {
        if (error) {
            [HJHUD showErrorMessage:@"Operation failed"];
            self.selectModel.isSelect = !self.selectModel.isSelect;
            [self.tableView reloadData];
            return;
        }
        
        if (self.complete) {
            self.complete(value);
        }

        HJUserModel *userModel = [HJUserModel currentAccountModel];
        userModel.region = value;
        [userModel bg_saveOrUpdateAsync:nil];
        
        [HJHUD showSucceedMessage:@"Successful operation"];
        [self.navigationController popViewControllerToController:@"HJPersonalCenterV2Controller"];
    }];
}

- (void)loadData {
    NSMutableArray *Marr = [NSMutableArray array];
    

    [self.citys enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [Marr addObject:[HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
            model.anyObject = obj;
            model.text = obj[@"cityName"];
            model.showArrow = NO;
            if ([self.region containsString:model.text]) {
                model.isSelect = YES;
            }
        }]];
    }];

    self.datas = Marr;
}
 

@end
